package com.app.aipass.fragment.my.problem;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.app.aipass.BR;
import com.app.aipass.R;
import com.app.aipass.app.AppViewModelFactory;
import com.app.aipass.databinding.FragmentProblemDetailBinding;
import com.app.aipass.entity.Article;
import com.app.aipass.utils.StatusBarUtil;
import com.app.aipass.utils.TextUtil;
import com.app.aipass.utils.WebViewUtil;

import java.util.Objects;

import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * @author: woking
 * @date: 2021/6/11
 * @description：
 */
public class ProblemDetailFragment extends BaseFragment<FragmentProblemDetailBinding, ProblemDetailViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_problem_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
    @Override
    public ProblemDetailViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(ProblemDetailViewModel.class);
    }
    @Override
    public void initData() {
        super.initData();
        if (getArguments() != null) {
            String id = getArguments().getString("id");
            String title = getArguments().getString("title");
            binding.tvTitleText.setText(title);
            if(TextUtil.isEmpty(id))
            {
                binding.contentTv.setVisibility(View.VISIBLE);
                binding.shopMessageWebview.setVisibility(View.GONE);
            }
            else
            {
                binding.contentTv.setVisibility(View.GONE);
                binding.shopMessageWebview.setVisibility(View.VISIBLE);
                viewModel.zdeskArtivle(id);
            }
        }
        binding.ivTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
//        viewModel.zdeskArtivle("201");
//        viewModel.requestNetWork("201");//常见问题201 信用规则202  处罚规则203

        initWebView();

        viewModel.mArticle.observe(this, new Observer<Article>() {
            @Override
            public void onChanged(Article article) {
                WebViewUtil.setWebView(binding.shopMessageWebview, Objects.requireNonNull(getContext()));
                WebViewUtil.loadHtml(binding.shopMessageWebview, article.content);
            }
        });
    }

    private void initWebView()
    {
        binding.shopMessageWebview.setWebViewClient(new WebViewClient() { //通过webView打开链接，不调用系统浏览器
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                if(isshouldOverrideUrl)
//                {
//                    view.loadUrl(url);
//                    return true;
//                }
                return false;
            }
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                // Toast.makeText(getApplicationContext(), "网络连接失败 ,请连接网络。", Toast.LENGTH_SHORT).show();
            }
        });
        WebSettings webSettings = binding.shopMessageWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(false);
//        if (null != url) {
//            Log.e("TAG","=======222======onPrepared======url======="+url);
//            binding.shopMessageWebview.loadUrl(url);
//        }
        binding.shopMessageWebview.setOnClickListener(v -> {
            if (binding.shopMessageWebview.canGoBack()) {
                binding.shopMessageWebview.goBack();
            }
        });
    }
}
